package com.alumni_circle.core.utils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author 龙小虬
 * @since 2020-08-15 14:28
 */
@RestController
public class UrlController {

    @Resource
    private RequestMappingHandlerMapping rMhm;

    /**
     * 首字母大写改小写
     *
     * @param s 字符串
     * @return 首字母小写字符串
     */
    private static String toLowerCaseFirstOne(String s){
        if(Character.isLowerCase(s.charAt(0))) {
            return s;
        }
        else {
            return Character.toLowerCase(s.charAt(0)) + s.substring(1);
        }
    }

    /**
     * 用于获取所有的controller接口名
     */
    @RequestMapping("/getAllUrl")
    public void getAllUrl(){
        Set<String> getUrls = new HashSet<>();
        System.out.println("1111");
        Map<RequestMappingInfo, HandlerMethod> handlermethod= rMhm.getHandlerMethods();
        Collection< HandlerMethod> methods = handlermethod.values();
        for (HandlerMethod method : methods) {
            if(method.toString().contains("com.alumni_circle")) {
                String url = method.toString();
                //com.educational_system.controller.AssessStudentController#addAssessStudent(AssessStudent)
                url = url.substring(url.lastIndexOf(".") + 1);
                //AssessStudentController#addAssessStudent(AssessStudent)
                url = toLowerCaseFirstOne(url);
                //assessStudentController#addAssessStudent(AssessStudent)
                url = url.substring(0, url.lastIndexOf("("));
                //assessStudentController#addAssessStudent
                url = url.replace("#", "/");
                //assessStudentController/addAssessStudent
                url = url.replace("Controller","");
                getUrls.add(url);
                System.out.println("set.add(\""+"/"+url+"\");");
            }
        }
    }

}
